package com.kantboot.system.user.service.impl;

import com.kantboot.system.user.dao.repository.entity.SysPermissionRoleRepository;
import com.kantboot.system.user.dao.repository.entity.SysRoleRepository;
import com.kantboot.system.user.domain.dto.SysRoleSearchDTO;
import com.kantboot.system.user.domain.entity.SysPermissionRole;
import com.kantboot.system.user.service.ISysRoleService;
import com.kantboot.util.core.param.PageParam;
import com.kantboot.util.core.result.PageResult;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class SysRoleServiceImpl implements ISysRoleService {

    @Resource
    private SysRoleRepository repository;

    @Resource
    private SysPermissionRoleRepository permissionRoleRepository;

    @Override
    public PageResult getBodyData(PageParam<SysRoleSearchDTO> pageParam) {
        return PageResult.of(repository.getBodyData(pageParam.getData(),pageParam.getPageable()));
    }

    @Override
    public List<String> getPermissionCodeListByRoleCode(String roleCode) {
        return permissionRoleRepository.findPermissionCodeListByRoleCode(roleCode);
    }

    @Override
    public void bindPermission(String roleCode, List<String> permissionCodeList) {
        permissionRoleRepository.deleteByRoleCode(roleCode);
        List<SysPermissionRole> permissionRoleList = new ArrayList<>();
        for(String permissionCode:permissionCodeList){
            SysPermissionRole permissionRole = new SysPermissionRole();
            permissionRole.setRoleCode(roleCode);
            permissionRole.setPermissionCode(permissionCode);
            permissionRoleList.add(permissionRole);
        }
        permissionRoleRepository.saveAll(permissionRoleList);
    }
}
